<?php

//////////////////////////////////////////////////////////////////////////
//
// WikiHiero - A PHP convert from text using "Manual for the encoding of 
// hieroglyphic texts for computer input" syntax to HTML entities (table and
// images).
//
// Copyright (C) 2004-2010 Guillaume Blanchard (Aoineko)
// 
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or any later version.
// 
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
// 
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
//
//////////////////////////////////////////////////////////////////////////

require "wh_config.php";
require "wh_dico.php";

define("WH_VER_MAJ", 2);
define("WH_VER_MED", 0);
define("WH_VER_MIN", 0);

if(array_key_exists("lang", $_GET))
	$lang = $_GET["lang"];
else
	$lang = "fr"; 

?>

<html lang=<?php echo $lang; ?>>
	<head>
		<title>WikiHiero - Table generator</title>
		<meta http-equiv="Content-type" content="text/html; charset=UTF-8">
		<link rel="shortcut icon" href="/favicon.ico">
		</head>
		<body bgcolor="#DDDDDD">

		<big><?php echo "WikiHiero v".WH_VER_MAJ.".".WH_VER_MED.".".WH_VER_MIN; ?></big> 

		<br><br>Parsing hieroglyph files and creating tables in "<?php echo WH_LIST_FILE; ?>"...<br><br>

    <?php
    
	function WH_GetCode($file)
	{
		return substr($file, strlen(WH_IMG_PRE), -(1 + strlen(WH_IMG_EXT)));
 	}

	function WH_GetConvert($file)
	{
		global $wh_phonemes;
		$text = WH_GetCode($file);
		$hieroList = explode("&", $text);
		foreach($hieroList as &$hiero)
		{
			if(array_key_exists($hiero, $wh_phonemes))
				$hiero = $wh_phonemes[$hiero];
		}
		$text = implode("&", $hieroList);
		return $text;
 	}

 	$wh_prefabs = "\$wh_prefabs = array(\n";
	$wh_files   = "\$wh_files   = array(\n";

	if(is_dir(WH_IMG_DIR))
	{
		if ($dh = opendir(WH_IMG_DIR))
		{
			while (($file = readdir($dh)) !== false) 
			{
				if(stristr($file, WH_IMG_EXT))
				{
					list($width, $height, $type, $attr) = getimagesize(WH_IMG_DIR.$file);
					$wh_files .= "  \"".WH_GetConvert($file)."\" => array( 'width' => $width, 'height' => $height, 'file' => '".WH_GetCode($file)."' ),\n";
					if(strchr($file,'&'))
						$wh_prefabs .= "  \"".WH_GetConvert($file)."\",\n";
				}
			}
			closedir($dh);
		}
	}

	$wh_prefabs .= ");";
	$wh_files .= ");";

	echo "<pre>$wh_prefabs<br><br>";
	echo "$wh_files<br><br></pre>";

	$file = fopen(WH_LIST_FILE, "w+");
	fwrite($file, "<?php\n\n");
	fwrite($file, "// File created by wh_generate.php version ".WH_VER_MAJ.".".WH_VER_MED.".".WH_VER_MIN."\n");
	fwrite($file, "// ".date("Y/m/d at H:i")."\n\n");
	fwrite($file, "$wh_prefabs\n\n");
	fwrite($file, "$wh_files\n\n");
	fwrite($file, "?>");
	fclose($file);

    //if(file_exists("wh_list(0).php"))
    
    ?>

    <small><?php /*echo WH_Credit();*/ ?></small>

  </body>
</html>